import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/login",
    name: "Home",
    component: Home
  },
  {
    path: "/register",
    name: "About",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/About.vue")
  },
  {
    path: "/:micro(app-vue-hash|pureHtml):endPath(.*)",
    name: "Child",
    component: () => import("../views/Child.vue")
  },
  {
    path: "/404",
    name: "404",
    component: () => import("../components/404.vue")
  }
];

const router = new VueRouter({
  routes
});

const childrenPath = ["/app-vue-hash", "/pureHtml"];
router.beforeEach((to, from, next) => {
  if (to.name) {
    // 有 name 属性，说明是主应用的路由
    return next();
  }
  if (childrenPath.some(item => to.path.includes(item))) {
    return next();
  }
  next({ name: "404" });
});

export default router;
